<template>
    <div class="mainContent">
        <ul class="ammeterList">
            <li :class="{'actived': item.id===deviceMsg.id}" v-for="item in deviceList" :key="item.id" @click="changeDevice(item)">{{item.name}}</li>
        </ul>
        <div class="ammeterContent">
            <div class="search">
                <span>日期选择：</span>
                <el-date-picker
                    type="date"
                    v-model="datePicker"
                    :picker-options="options"
                    @change="dateChange"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期">
                </el-date-picker>
                <input type="text" class="hiddenInput" />
            </div>
            <div class="ammeterMsg">
                <div class="code">
                    <div class="value">{{deviceMsg.sn}}</div>
                    <div class="label">设备编号</div>
                </div>
                <div class="code">
                    <div class="value">{{deviceMsg.name}}</div>
                    <div class="label">设备名称</div>
                </div>
                <div class="stateWrap">
                    <div class="value" v-if="deviceData.muteStatus===1">已消音</div>
                    <div class="value" v-else-if="deviceData.muteStatus===0">未消音</div>
                    <div class="value" v-else>--</div>
                    <div class="label">消音状态</div>
                </div>
                <div class="stateWrap">
                    <div class="value" v-if="deviceData.relayOutputStatus===1">已输出</div>
                    <div class="value" v-else-if="deviceData.relayOutputStatus===0">未输出</div>
                    <div class="value" v-else>--</div>
                    <div class="label">继电器状态</div>
                </div>
                <div class="stateWrap">
                    <div class="value">{{deviceData.phaseLossStatus | getLabel(stateList)}}</div>
                    <div class="label">缺项状态</div>
                </div>
                <div class="stateWrap">
                    <div class="value">{{deviceData.powerOffStatus | getLabel(stateList)}}</div>
                    <div class="label">断电状态</div>
                </div>
                <div class="turnoverTime">
                    <div class="value">{{deviceData.createTime | isNull}}</div>
                    <div class="label">数据采集时间</div>
                </div>
            </div>
            <div class="ammeterData">
                <div class="dataWrap">
                    <div class="wrapTitle">
                        <img :src="powerIcon" />
                        <span>电能</span>
                    </div>
                    <div class="dataItem">
                        <p class="label">A相电能：</p>
                        <p class="value">{{deviceData.aphaseElectricalEnergy | isNull}}KW · H</p>
                    </div>
                    <div class="dataItem">
                        <p class="label">B相电能：</p>
                        <p class="value">{{deviceData.bphaseElectricalEnergy | isNull}}KW · H</p>
                    </div>
                    <div class="dataItem">
                        <p class="label">C相电能：</p>
                        <p class="value">{{deviceData.cphaseElectricalEnergy | isNull}}KW · H</p>
                    </div>
                </div>
                <div class="dataWrap">
                    <div class="wrapTitle">
                        <img :src="currentsIcon" />
                        <span>电流/电频</span>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">A相电流：</p>
                            <p class="value">{{deviceData.aphaseCurrent | isNull}}A</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">A相频率：</p>
                            <p class="value">{{deviceData.aphaseFrequence | isNull}}Hz</p>
                        </div>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">B相电流：</p>
                            <p class="value">{{deviceData.bphaseCurrent | isNull}}A</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">B相频率：</p>
                            <p class="value">{{deviceData.bphaseFrequence | isNull}}Hz</p>
                        </div>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">C相电流：</p>
                            <p class="value">{{deviceData.cphaseCurrent | isNull}}A</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">C相频率：</p>
                            <p class="value">{{deviceData.cphaseFrequence | isNull}}Hz</p>
                        </div>
                    </div>
                </div>
                <div class="dataWrap">
                    <div class="wrapTitle">
                        <img :src="voltageIcon" />
                        <span>电压</span>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">A相过载状态：</p>
                            <p class="value" :class="{'warning': deviceData.aphaseOverloadStatus!=0}">{{deviceData.aphaseOverloadStatus | getLabel(stateList)}}</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">A相电压：</p>
                            <p class="value">{{deviceData.aphaseVoltage | isNull}}V</p>
                        </div>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">B相过载状态：</p>
                            <p class="value" :class="{'warning': deviceData.bphaseOverloadStatus!=0}">{{deviceData.bphaseOverloadStatus | getLabel(stateList)}}</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">B相电压：</p>
                            <p class="value">{{deviceData.bphaseVoltage | isNull}}V</p>
                        </div>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">C相过载状态：</p>
                            <p class="value" :class="{'warning': deviceData.cphaseOverloadStatus!=0}">{{deviceData.cphaseOverloadStatus | getLabel(stateList)}}</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">C相电压：</p>
                            <p class="value">{{deviceData.cphaseVoltage | isNull}}V</p>
                        </div>
                    </div>
                </div>
                <div class="dataWrap">
                    <div class="wrapTitle">
                        <img :src="tempIcon" />
                        <span>温度</span>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">A相温度状态：</p>
                            <p class="value" :class="{'warning': deviceData.aphaseTemperatureStatus!=0}">{{deviceData.aphaseTemperatureStatus | getLabel(stateList)}}</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">A相温度：</p>
                            <p class="value">{{deviceData.aphaseTemperature}}℃</p>
                        </div>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">B相温度状态：</p>
                            <p class="value" :class="{'warning': deviceData.bphaseTemperatureStatus!=0}">{{deviceData.bphaseTemperatureStatus | getLabel(stateList)}}</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">B相温度：</p>
                            <p class="value">{{deviceData.bphaseTemperature}}℃</p>
                        </div>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">C相温度状态：</p>
                            <p class="value" :class="{'warning': deviceData.cphaseTemperatureStatus!=0}">{{deviceData.cphaseTemperatureStatus | getLabel(stateList)}}</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">C相温度：</p>
                            <p class="value">{{deviceData.cphaseTemperature}}℃</p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="ammeterData">
                <div class="dataWrap">
                    <div class="wrapTitle">
                        <img :src="overtensionIcon" />
                        <span>过压/负压</span>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">过压状态：</p>
                            <p class="value" :class="{'warning': deviceData.overVoltageStatus!=0}">{{deviceData.overVoltageStatus | getLabel(stateList)}}</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">负压状态：</p>
                            <p class="value" :class="{'warning': deviceData.underVoltageStatus!=0}">{{deviceData.underVoltageStatus | getLabel(stateList)}}</p>
                        </div>
                    </div>
                </div>
                <div class="dataWrap">
                    <div class="wrapTitle">
                        <img :src="leakageIcon" />
                        <span>漏电</span>
                    </div>
                    <div class="double">
                        <div class="dataItem">
                            <p class="label">漏电状态：</p>
                            <p class="value" :class="{'warning': deviceData.electricityLeakageStatus!=0}">{{deviceData.electricityLeakageStatus | getLabel(stateList)}}</p>
                        </div>
                        <div class="dataItem">
                            <p class="label">漏电流：</p>
                            <p class="value">{{deviceData.leakCurrent | isNull}}mA</p>
                        </div>
                    </div>
                </div>
                <div class="dataWrap bg"></div>
                <div class="dataWrap bg"></div>
            </div>
        </div>
    </div>
</template>

<script>
import { format } from "@/sever/utils.js";
import { getEnergyDeviceList, getAmmeterDetail } from "@/api/energy.js";

export default {
    name: 'ammeterData',
    data() {
        return {
            powerIcon: require("@/assets/images/energy/电能.png"),
            currentsIcon: require("@/assets/images/energy/电流.png"),
            voltageIcon: require("@/assets/images/energy/电压.png"),
            tempIcon: require("@/assets/images/energy/温度.png"),
            overtensionIcon: require("@/assets/images/energy/负压.png"),
            leakageIcon: require("@/assets/images/energy/漏电.png"),

            options: {
                disabledDate(date) {
                    return date > Date.now();
                }
            },
            datePicker: format(new Date(), "YYYY-MM-DD"),

            deviceType: 14,
            deptId: "",

            deviceMsg: '',
            deviceList: [],
            deviceData: {},

            /* 状态枚举 */
            stateList: [
                {key: 0, value: "正常"},
                {key: 1, value: "已消音"},
                {key: 2, value: "报警"},
                {key: 3, value: "故障"},
            ]
        };
    },
    computed: {
        porjectObject() {
            return this.$store.state.porjectObject;
        },
    },
    watch: {
        porjectObject(val, oldVal) {
            if (val.deptId != oldVal.deptId) {
                this.deptId = val.deptId;
                this.getDeviceList();
            }
        },
    },
    mounted() {
        this.deptId = this.$store.state.porjectObject.deptId
        this.getDeviceList()
    },
    methods:{
        /* 设备切换 */
        changeDevice(val){
            this.deviceData = {}
            this.deviceMsg = val
            this.getDeviceData()
        },

        /* 日期选择 */
        dateChange(val){
            this.deviceData = {}
            this.datePicker = val
            this.getDeviceData()
        },

        /* 设备列表 */
        getDeviceList(){
            let param = {
                deptId: this.deptId,
                type: this.deviceType
            }
            getEnergyDeviceList(param).then(res=>{
                if(res.code == 200){
                    if(res.data.length>0){
                        this.deviceList = res.data
                        this.deviceMsg = res.data[0]
                        this.getDeviceData()
                    }
                }else{
                    this.$Message.warning(res.msg)
                }
            })
        },

        /* 设备详情 */
        getDeviceData(){
            let param = {
                deviceId: this.deviceMsg.id,
                time: this.datePicker
            }
            getAmmeterDetail(param).then(res=>{
                if(res.code == 200){
                    this.deviceData = res.data ? res.data : {}
                }
            })
        }
    },
    filters:{
        getLabel(key, list){
            let label = "--"
            list.forEach(element => {
                if(element.key === key){
                    label = element.value
                }
            })
            return label
        },
        isNull(value){
            if(!value&&value!=0){
                return "--"
            }
            return value
        }
    }
}
</script>
<style scoped lang='less'>

@import "./../../assets/style/themeBase.less";
.mainContent {
    width: 100%;
    height: 100%;
    padding: 10px;
    box-sizing: border-box;
    background: #f5f5f5;
    display: flex;
    .ammeterList{
        padding: 10px 20px;
        border-radius: 5px;
        background-color: #fff;
        list-style: none;
        li{
            cursor: pointer;
            padding: 5px;
            width: 200px;
            font-size: 14px;
            color: #606266;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .actived{
            color: var(--themeColor);
        }
    }
    .ammeterContent{
        margin-left: 20px;
        flex: 1;
        .search{
            margin-bottom: 10px;
            padding: 15px 20px;
            border-radius: 4px;
            background-color: #fff;
        }
        .ammeterMsg{
            padding: 15px 0;
            border-radius: 4px;
            background-color: #fff;
            display: flex;
            .code{
                width: 200px;
                border-right: 1px solid #eee;
                box-sizing: border-box;
                text-align: center;
                .label{
                    font-size: 14px;
                    color: #666666;
                    margin-top: 5px;
                }
                .value{
                    font-size: 16px;
                    font-weight: bold;
                    color: #1891ff;
                }
            }
            .stateWrap{
                flex: 1;
                // padding: 0 30px;
                text-align: center;
                .label{
                    font-size: 14px;
                    color: #666666;
                    margin-top: 5px;
                }
                .value{
                    font-size: 16px;
                    color: #1891ff;
                }
            }
            .turnoverTime{
                width: 220px;
                text-align: center;
                .label{
                    font-size: 14px;
                    color: #666666;
                    margin-top: 5px;
                }
                .value{
                    font-size: 16px;
                    color: #1891ff;
                }
            }
        }
        .ammeterData{
            margin-top: 10px;
            display: flex;
            .dataWrap{
                flex: 1;
                margin-right: 10px;
                padding: 20px;
                background-color: #fff;
                border-radius: 4px;
                .wrapTitle{
                    margin-bottom: 5px;
                    display: flex;
                    align-items: center;
                    img{
                        margin-right: 10px;
                        width: 40px;
                    }
                    span{
                        font-size: 14px;
                        color: #333;
                        font-weight: bold;
                    }
                }
                .dataItem{
                    padding: 10px 0;
                    .label{
                        margin-bottom: 2px;
                        font-size: 14px;
                        color: #606266;
                        line-height: 1;
                    }
                    .value{
                        font-size: 16px;
                        color: #1891ff;
                        font-weight: bold;
                        line-height: 1;
                    }
                    .warning{
                        color: orange;
                    }
                }
                .double{
                    display: flex;
                    .dataItem{
                        flex: 1;
                    }
                }
            }
            .bg{
                background-color: #EBF2F8;
            }
        }
        .emptyBox{
            padding-top: 50px;
        }
    }

    .hiddenInput{
        opacity:0;
        position: absolute;
        top:-9999px;
        width:0;
    }
}
</style>
